var createError = require('http-errors');
var express = require('express');
//引入跨域的中间件
var cors = require('cors');

var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const swaggerInstall = require('./utils/swagger');


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var loginRouter = require("./routes/login");
//引入产品路由
var productRouter = require('./routes/product');
// 引入菜单路由 
var menuRouter = require('./routes/menus');
//引入栏目路由
var columnRouter = require('./routes/column');
//引入轮播接口
var carouselRouter = require('./routes/carousel');
//引入顾客接口
var customerRouter = require('./routes/customer');
//引入员工接口
var employeeRouter = require('./routes/employee');
//引入系统管理接口
var systemRouter = require('./routes/system');


var app = express();


swaggerInstall(app);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/user',loginRouter);
//使用产品路由
app.use('/product',productRouter);
// 使用菜单路由
app.use('/menus',menuRouter);
//使用栏目路由
app.use('/column',columnRouter);
//使用轮播路由
app.use('/carousel',carouselRouter);
//使用顾客路由
app.use('/customer',customerRouter);
//使用员工路由
app.use('/employee',employeeRouter);
//使用系统路由
app.use('/system',systemRouter);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
